package com.centerserver;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.MulticastSocket;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.DefaultListModel;


class RecordVideoThread extends Thread {

	private ServerSocket mServerSocket;
	private DatabaseManager mDatabaseManager;
	private Map<String, ProcessRtpPacketThread> mThreadMap;
	private DatagramPacketQueue mDatagramPacketQueue;
//	private Map<String, MulticastSocket> mMulticastSocketMap;
//	private Map<String, InetAddress> mInetAddrMap;
	private int mCurCaptureEndNum;
	private DefaultListModel mCaptureVideoItemListModel;
	private Map<String, List<String>> mMonitorListMap;

	public RecordVideoThread(
			DatabaseManager databaseManager, 
			DefaultListModel captureVideoItemListModel,
			Map<String, List<String>> monitorListMap/*,
			Map<String, InetAddress> inetAddrMap*/) {
		
		mCurCaptureEndNum = 0;
		mThreadMap = new HashMap<String, ProcessRtpPacketThread>();
//		mMulticastSocketMap = new HashMap<String, MulticastSocket>();
//		mInetAddrMap = inetAddrMap;
		mDatagramPacketQueue = new DatagramPacketQueue();
		mDatabaseManager = databaseManager;
		mCaptureVideoItemListModel = captureVideoItemListModel;
		mMonitorListMap  = monitorListMap;
		
		try {
			mServerSocket = new ServerSocket(
					CenterServerConfig.CAPTURE_END_SENDTO_TCP_PORT);
		} catch (IOException e) {
			System.out.println("socket establied successfully!");
			e.printStackTrace();
		}
	}

	@Override
	public void run() {

		Socket client;
		boolean recvRtpThreadStarted = false;
		try {
			while (true) {

				client = mServerSocket.accept();
				
				String ipAddress = ((InetSocketAddress)client.getRemoteSocketAddress()).getAddress().getHostAddress();
		
				ProcessRtpPacketThread processRtpPacketThread = new ProcessRtpPacketThread();
				mThreadMap.put(ipAddress, processRtpPacketThread);
				processRtpPacketThread.start();

				mCurCaptureEndNum++;
				
//				// distribute one multicast address for every capture end
//				mInetAddrMap.put(ipAddress, 
//						InetAddress.getByName(
//								CenterServerConfig.CONFIG_MULTICAST_TWO_HIGH_BYTES.
//								concat("" + mCurCaptureEndNum/256).concat(".")
//								.concat("" + mCurCaptureEndNum%256)));
//				
//				System.out.println("Debug: multicastAddr: " 
//						+ CenterServerConfig.CONFIG_MULTICAST_TWO_HIGH_BYTES.
//						concat("" + mCurCaptureEndNum/256).concat(".")
//						.concat("" + mCurCaptureEndNum%256));
				
				
				RecvSpsPpsThread recvSpsPpsThread = new RecvSpsPpsThread(
						client, mDatabaseManager, mThreadMap, mCaptureVideoItemListModel);
				recvSpsPpsThread.start();				
				
				if( !recvRtpThreadStarted ) {
					recvRtpThreadStarted = true;
					new DispathRtpPacketThread(
							mDatabaseManager,
							mDatagramPacketQueue, 
							mThreadMap,
							mMonitorListMap/*, 
							mInetAddrMap*/).start();
					new RecvSequentialNaluThread(mDatagramPacketQueue).start();
					
				}
				
			}
		} catch (IOException e) {
			System.out.println("socket exception while accepting");
			e.printStackTrace();
		}
		finally {
			try {
				if( null != mServerSocket)
					mServerSocket.close();
			} catch (IOException e) {
				System.out.println("Record Video Socket1 closed");
				e.printStackTrace();
			}
		}
	}
}
